package com.foreverwin.jzymes.eap.Listener;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.service.ICncReqLogService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.framework.annotation.MessageLog;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.qc.check.service.IQcCheckAdiviceService;
import com.foreverwin.jzymes.qc.check.service.IQcCheckService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class QcCheckListener {

    private Logger logger = LoggerFactory.getLogger( QcCheckListener.class );

    @Autowired
    IQcCheckAdiviceService qcCheckAdiviceService;
    @Autowired
    IQcCheckService qcCheckService;
    @Autowired
    ICncReqLogService cncReqLogService;
    @Autowired
    IResourceAdviceService resourceAdviceService;

    @JmsListener( destination = "MES_QC_CHECK_REPLY" )
    @MessageLog
    public void receiveQcCheckFromQueue( String qcCheckJson ) {
        logger.info( "接收到抽检本体信息: " + qcCheckJson );
        JSONObject map = null;
        try {
            map = JSONObject.parseObject( qcCheckJson );
            String site = map.getString( "SITE" );
            String taskId = map.getString( "TASK_ID" );
            String resource = map.getString( "RESRCE" );
            String carrier = CarrierUtil.buildCarrier( site, map.getString( "CARRIER" ) );
            Date sendTime = DateUtil.parse( map.getString( "SEND_TIME" ) );
            qcCheckAdiviceService.retrieveQcCheckCarrier( site, carrier, resource, taskId, sendTime );
            logger.info( "抽检本体信息处理完成: " + qcCheckJson );
        } catch (Exception e) {
            logger.error( "抽检本体信息处理失败: " + e.getMessage() + "，抽检本体信息: " + qcCheckJson );
            throw e;
        }
    }


    @JmsListener( destination = "MES_FAI_INFRA" )
    @MessageLog
    public void faiDataFromQueue( String faiJson ) {
        logger.info( "接收到三次元测量数据信息: " + faiJson );
        JSONObject map = null;
        try{
            map = JSONObject.parseObject( faiJson );
            String faiId = map.getString( "FAI_ID" );
            qcCheckAdiviceService.processfaiData( faiId );
            logger.info( "三次元测量数据信息处理完成: " + faiId );
        }catch ( Exception e ){
            logger.error( "三次元测量数据信息处理失败: " + e.getMessage() + "，三次元测量数据信息: " + faiJson );
            throw e;
        }
    }
}
